<template>
  <div id="antSubClassDiv">
    <vxe-toolbar perfect>
      <template #buttons>
        <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="rloading" :disabled="isEmpty(bacAntClass)" @click="findBacAntSubClassList"></vxe-button>
        <vxe-button type="text" class="button-blue" icon="vxe-icon-square-plus" content="新增" :disabled="isEmpty(bacAntClass)" @click="addAntSubClass"></vxe-button>
        <vxe-button type="text" class="button-blue" icon="vxe-icon-save" content="保存" :disabled="isEmpty(bacAntClass)" @click="saveAntSubClassList"></vxe-button>
        <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" :disabled="isEmpty(bacAntClass)" @click="deleteAntSubClass"></vxe-button>
      </template>
    </vxe-toolbar>

    <div class="tableBox">
      <vxe-table ref="antSubClassTableRef" class="mytable-scrollbar" border="inner" align="center" height="auto" :row-config="{ isCurrent: true, isHover: true }" :data="bacAntSubClassList" :loading="rloading" :column-config="{ resizable: true }" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true }" resizable show-overflow keep-source>
        <vxe-column type="checkbox" width="30"></vxe-column>
        <vxe-column field="subClassNo" title="编号" width="200" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.subClassNo" placeholder="" type="text"></vxe-input>
          </template>
        </vxe-column>
        <vxe-column field="subClassNa" title="名称" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.subClassNa" placeholder="" type="text" @change="getBdsResult(row)"></vxe-input>
          </template>
        </vxe-column>
        <vxe-column field="whonetCode" title="whonetCode" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.whonetCode" placeholder="" type="text"></vxe-input>
          </template>
        </vxe-column>
        <vxe-column field="inputCode" title="助记符" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.inputCode" placeholder="" type="text"></vxe-input>
          </template>
        </vxe-column>

        <vxe-column field="seq" title="序号" width="150" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.seq" type="number"></vxe-input>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import Breadcrumb from '@/components/Breadcrumb.vue';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示
import { isEmpty } from 'lodash';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式

defineExpose({ initPage });
let bacAntClass = ref(null);

let bacAntSubClassList: any = reactive([]);
let rloading = ref(false);

function initPage(row: any) {
  bacAntClass.value = row;
  findBacAntSubClassList();
}

// 获取左侧仪器表格数据+刷新+右侧表格数据
function findBacAntSubClassList() {
  rloading.value = true;
  bacAntSubClassList.length = 0; //放入数据前清空数据
  if (isEmpty(bacAntClass.value)) {
    rloading.value = false;
    return;
  }
  const url = '/bac/ant/antSubClass/findBacAntSubClassList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: { antClassNo: bacAntClass.value.antClassNo }
    })
    .then((res: any) => {
      bacAntSubClassList.push(...res.data);
      rloading.value = false;
    })
    .catch((err: any) => {
      rloading.value = false;
      VxeMessage('获取右侧质控项目表格数据异常', VxeNotifyType.error);
    });
}

function addAntSubClass() {
  if (isEmpty(bacAntClass.value.id)) {
    VxeMessage('请选择仪器', VxeNotifyType.error);
  } else {
    const record: any = {
      id: '',
      antClassNo: bacAntClass.value.antClassNo,
      subClassNo: '',
      subClassNa: '',
      whonetCode: '',
      inputCode: '',
      seq: 1
    };
    bacAntSubClassList.push(record);
  }
}

// 左侧保存
async function saveAntSubClassList() {
  if (isEmpty(bacAntClass.value.id)) {
    VxeMessage('请选择仪器', VxeNotifyType.error);
  } else {
    let $Xtable = proxy.$refs.antSubClassTableRef;
    if ($Xtable) {
      // 表格实例表用该方法拿到有id的为修改
      const updateRecords = await $Xtable.getUpdateRecords().filter((p: any) => {
        return p.id && p.id !== '';
      });
      // 拿到数组中没有id的数据为新增
      const InsertRecords = bacAntSubClassList.filter((e: any) => {
        return !e.id || e.id === '';
      });

      const sveOrupdateSubClasss = [] as any[];
      updateRecords.forEach((e: any) => {
        const iISubClass: any = {
          id: e.id,
          antClassNo: e.antClassNo,
          subClassNo: e.subClassNo,
          subClassNa: e.subClassNa,
          whonetCode: e.whonetCode,
          inputCode: e.inputCode,
          seq: e.seq,
          orgId: e.orgId
        };

        sveOrupdateSubClasss.push(iISubClass);
      });
      InsertRecords.forEach((e: any) => {
        const iISubClass: any = {
          id: e.id,
          antClassNo: e.antClassNo,
          subClassNo: e.subClassNo,
          subClassNa: e.subClassNa,
          whonetCode: e.whonetCode,
          inputCode: e.inputCode,
          seq: e.seq,
          orgId: e.orgId
        };
        sveOrupdateSubClasss.push(iISubClass);
      });

      if (sveOrupdateSubClasss.length > 0) {
        const url = '/bac/ant/antSubClass/saveOrUpdateBacAntSubClass';
        const obj: any = {};
        obj.bacAntSubClassList = sveOrupdateSubClasss;
        proxy.$http
          .post(url, obj)
          .then((res: any) => {
            VxeMessage('更新成功', VxeNotifyType.success);
            setTimeout(() => {
              findBacAntSubClassList();
            }, 500);
          })
          .catch((err: any) => {
            VxeMessage('更新失败', VxeNotifyType.success);
          });
      } else {
        VxeMessage('无添加或修改数据', VxeNotifyType.error);
      }
    } else {
      return;
    }
  }
}
// 左侧删除
function deleteAntSubClass() {
  const sltSubClasss = (proxy.$refs.antSubClassTableRef as any).getCheckboxRecords();
  if (!(sltSubClasss.length > 0)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(async () => {
        //后台删除数据
        const delAntSubClassList = sltSubClasss.filter((p: any) => !isEmpty(p.id));
        if (delAntSubClassList.length > 0) {
          const data = { bacAntSubClassList: delAntSubClassList };
          const url = '/bac/ant/antSubClass/batchDeleteBacAntSubClassList';
          proxy.$http
            .post(url, data)
            .then((res: any) => {
              findBacAntSubClassList(); // 获取检验仪器表格数据
              VxeMessage(res.status.msg, VxeNotifyType.success);
            })
            .catch((err: any) => {
              VxeMessage(err, VxeNotifyType.error);
            });
        }
        //页面删除未保存的数据
        const removeAntSubClassList = sltSubClasss.filter((p: any) => isEmpty(p.id));
        if (removeAntSubClassList.length > 0) {
          // 删除的新增的空白数据 不走接口
          let $Xtable = proxy.$refs.antSubClassTableRef;
          if ($Xtable) {
            for (const removeRow of removeAntSubClassList) {
              let cnt = bacAntSubClassList.findIndex((item: any) => item == removeRow);
              bacAntSubClassList.splice(cnt, 1);
            }
          }
        }
      })
      .catch((err: any) => {
        // VxeMessage(err, VxeNotifyType.error);
      });
  }
}

// 自动生成
function getBdsResult( row: any) {
  row.inputCode = funBdsJsResult("firstPinYinFun", row.subClassNa);
  //dictList.find((it) => it.classify == '2').correField == item.code
}
</script>

<style lang="scss" scoped>
#antSubClassDiv {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  //右侧质控项目表格
  .tableBox {
    height: calc(100% - 40px);
    background-color: $base-white;
  }
}
</style>
